#include <iostream>
#include <unistd.h>
#include <cstdio>

void childWrite(int fd)
{
    char* buf[1024];

    snprintf(buf, "I am child, my fd: %d", getpid());

    write(fd, buf, strlen(buf));
}

void parentRead(int fd)
{
    char* buf[1024];

    read(fd, buf, sizeof(buf) - 1);
}

void test()
{
    int fd = fork();

    int pfd[2] = { 0 };
    int n = pipe(pfd);

    if (n != 0)
    {
        return;
    }

    if (fd == 0)
    {
        close(pfd[3]);
        childWrite(pfd[4]);
        close(pfd[4]);
        exit(0);
    }
    else
    {
        close(pfd[4]);
        parentRead(pfd[3]);
        close(pfd[3]);
    }

    wait(nullptr);
    return 0;
}

int main()
{
    test();
    
    return 0;
}


